#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <stdio.h>
#include <assert.h>
#include <math.h>
#include "tw2815.h"

// arm-hisi-linux-gcc -o tw2815_test tw2815_test.c 

void printhelp(void)
{
	printf("1--pring all register values.\n" );
	printf("2--write a register\n" );
	printf("3--read a register\n" );	
}

int main(int argc,char* argv[])
{
	int dev;
	unsigned char reg;
	unsigned char value;
	tw2815_w_reg	write_reg;
	int para ;
	
	if (argc != 2)
	{
    	printf("usage :%s 1|2|3\n",argv[0]);
		printhelp();
		return 0;
	}
	
	dev = open("/dev/misc/tw2815adev", O_RDONLY);
	if (dev <= 0) {
		printf("tw2815adev device open fail\n");
		exit(0);
	}

	para = atoi(argv[1]);
	switch(para)
	{
		case 1:	
		{
			/* 
			 * read all register value . for example 
			 */
			ioctl(dev, TW2815_REG_DUMP);
			break ;
		}

		case 2:	
		{
			// write single register . for example
			printf("[write...]\tintput register number and value:") ;
			int reg1;
			int value1 ;
			scanf("%x %x",&reg1,&value1);			
			
			write_reg.addr = (unsigned char)reg1;
			write_reg.value = (unsigned char)value1;			
			ioctl(dev, TW2815_WRITE_REG, &write_reg);

			ioctl(dev, TW2815_READ_REG, &reg1);
			printf(" write register:reg = 0x%02x,value = 0x%02x\n", write_reg.addr, reg1);

			//printf("write register:reg = 0x%02x,value = 0x%02x \n",write_reg.addr,write_reg.value);
			
			break ;
		}
		
		case 3:	
		{
			// read single register . for example
			printf("[read...]\tintput register number:") ;
			int reg1;
			scanf("%x",&reg1);				
			
			reg = (unsigned char)reg1 ;
			unsigned char reg_no = reg ;
			value = ioctl(dev, TW2815_READ_REG, &reg);
			printf(" read register:reg = 0x%02x,value = 0x%02x\n", reg_no, reg);
			
			break ;
		}
		
		default :
			ioctl(dev, TW2815_REG_DUMP);
			break ;
	}

	close(dev);

	return 0;
}
